/**
 * @Description: 不同类型的影像提供加载器
 * @Author: Darren_yan
 * @Date: 2025-06-20 14:09:52
 * @LastEditors: Darren_yan
 * @LastEditTime: 2025-06-20 13:45:52
 */
import { isExist } from '../tools/index.js'
import { globeCollection } from './map_collection.js'
//移除的影像 统一调用的代码
function removeImageryLayerBylayer(layer){
    if (layer) {
		try {
			//web端使用removeImageryLayer移除底图会报错，但效果已生效
			window.viewer.scene.globe.removeImageryLayer(
				layer
			);
		} catch (error) {}
		layer.delete(); //delete是为了销毁底图，释放内存
	}
}

//删除 ArcGis影像服务
function removeArcGisImageryLayerById(id){
   if(isExist(id) && globeCollection.arcGis[id]){
        removeImageryLayerBylayer(globeCollection.arcGis[id])
		delete globeCollection.arcGis[id]
   }
}

//添加 ArcGis影像服务
function addArcGisImageryLayer({ id , url , isRomve = false }) {
	if (isExist(url) && isExist(id)) {
		if (!isExist(id)) {
			// throw new Error('id 不能为空')
			 console.error("id 不能为空")
			return
		}

		if (globeCollection.arcGis[id]) {
		  console.error("id 已存在")
			return
		}				
		globeCollection.arcGis[id] =
			window["viewer"].scene.globe.addArcGisMapServerImageryLayer(url)
	}else{
		console.error("影像缺少对应参数，请补全")
	}
}

//隐藏 ArcGis影像服务
function showArcGisImageryLayer({id,show}){
   if(isExist(id) && isExist(globeCollection.arcGis[id])){
      globeCollection.arcGis[id].show = show;
   }else{
	console.error("影像没有加载无法控制显示隐藏")
   }
}

//添加天地图 影像图层
function addTdtMapImageLayer({
	id,
	url,
	layerName= "img_c",
	token,
	tileWidth = 256,
	tileHeight = 256,
	minimumLevel = 1,
	maximumLevel = 17,
	hasAlphaChannel = false,
	useWebMercator = false,
	isLabel = false,
}) {
	if (!isExist(id)) {
		console.error("id 不能为空")
		return
	}

	if (globeCollection.tMap[id]) {
		// throw new Error('id 已存在')
		console.error("id 已存在")
		return
	}
	if (!isExist(token)) {
		// throw new Error('token 不能为空')
		console.error("token 不能为空")
		return
	}

	const tiandituImageryLayer = new SSmap.TiandituImageryLayer()
  
	if (isExist(url)) {
		let newUrl = `${url}?T=${layerName}&x={x}&y={y}&l={z}&tk=${token}`
		tiandituImageryLayer.url = newUrl
	} else {
		// throw new Error('url 不能为空')
		console.error("url 不能为空")
		return
	}

	if (isExist(tileWidth)) tiandituImageryLayer.tileWidth = tileWidth
	if (isExist(tileHeight)) tiandituImageryLayer.tileHeight = tileHeight
	if (isExist(minimumLevel)) tiandituImageryLayer.minimumLevel = minimumLevel
	if (isExist(maximumLevel)) tiandituImageryLayer.maximumLevel = maximumLevel
	if (isExist(hasAlphaChannel)) tiandituImageryLayer.hasAlphaChannel = hasAlphaChannel
	if (isExist(useWebMercator)) tiandituImageryLayer.useWebMercator = useWebMercator
	if (isExist(isLabel)) tiandituImageryLayer.isLabel = isLabel

	tiandituImageryLayer.componentComplete()
	globeCollection.tMap[id] = tiandituImageryLayer
}

//显示 隐藏 天地图 影像图层
function showTdtMapImageLayer({id,show}){
   if(isExist(id) && isExist(globeCollection.tMap[id])){
      globeCollection.tMap[id].enabled = show;
   }else{
	console.error("影像没有加载无法控制显示隐藏")
   }
}


//删除 天地图影像服务
function removeTdtMapImageLayerById(id){
   if(isExist(id) && globeCollection.tMap[id]){
			removeImageryLayerBylayer(globeCollection.tMap[id])
			delete globeCollection.tMap[id]
   }
}


export { addArcGisImageryLayer,showArcGisImageryLayer,removeArcGisImageryLayerById,addTdtMapImageLayer,showTdtMapImageLayer,removeTdtMapImageLayerById }